<?php
declare (strict_types=1);

namespace app\listener;

use app\common\model\Consulting;
use app\common\model\ConsultingMessage;
use app\index\service\MessageSendService;

class ConsultingServerRefund
{
    /**
     * 服务退款回收 事件监听处理
     * @param $params
     * @return void
     */
    public function handle($params)
    {
        $consulting = $params['consulting'] ?? null;
        $server = $params['server'] ?? null;
        $serverSku = $params['serverSku'] ?? null;

        if (is_null($consulting) || is_null($server)) {
            // TODO 记录错误日志

            return;
        }

        // 服务回收

        $now = time();

        // ************** 聊天时长和次数 ******************

        //

        $isChatFreely = $server->isChatFreely;
        $chatFreelyDuration = $server->chatFreelyDuration;
        $questionsNum = $server->questionsNum;
        if ($serverSku) {
            $chatFreelyDuration = $serverSku->chatFreelyDuration;
            $questionsNum = $serverSku->questionsNum;
        }

        // 给对应的咨询扣除聊天时长和次数
        $this->handleTimeAndNum($consulting, $now, $isChatFreely, $chatFreelyDuration, $questionsNum);

    }

    /**
     * 聊天时长和次数
     * @param Consulting $consulting
     * @param $now
     * @param $isChatFreely
     * @param $chatFreelyDuration
     * @param $questionsNum
     * @return void
     */
    private function handleTimeAndNum(Consulting $consulting, $now, $isChatFreely, $chatFreelyDuration, $questionsNum)
    {

        // 对应的咨询扣除聊天时长和次数
        if ($isChatFreely == '1') {
            // 畅聊

            $isChatFreelyEndTime = $consulting->isChatFreelyEndTime;
            if ($isChatFreelyEndTime < $now) {
                return;
            }

            $isChatFreelyEndTime -= $chatFreelyDuration * 3600;

            $consulting->isChatFreelyEndTime = $isChatFreelyEndTime;
            $consulting->questionsNumRemind = 0;
            $consulting->save();

            // 给用户消息
            if ($isChatFreelyEndTime > $now) {
                $content = [
                    'templates' => [
                        ['type' => 'text', 'red' => '102', 'text' => '退款成功，当前可用聊天时长',],
                        ['type' => 'text', 'red' => '101', 'text' => humanized_time_length($consulting->isChatFreelyEndTime - $now),],
                        ['type' => 'text', 'red' => '102', 'text' => '畅聊截止时间为：' . date('Y-m-d H:i', $consulting->isChatFreelyEndTime),],
                    ],
                ];
                $this->getMessageSendService()->sendSystemMessage($consulting, $consulting->memberId, ConsultingMessage::SYSTEM, $content);

                // 给律师消息
                if (is_id($consulting->lawyerId)) {
                    $content = [
                        'templates' => [
                            ['type' => 'text', 'red' => '102', 'text' => '用户退款成功,畅聊截止时间为：' . date('Y-m-d H:i', $consulting->isChatFreelyEndTime),]
                        ],
                    ];
                    $this->getMessageSendService()->sendSystemMessage($consulting, $consulting->lawyerId, ConsultingMessage::SYSTEM, $content);
                }
            } else {
                $content = [
                    'templates' => [
                        ['type' => 'text', 'red' => '102', 'text' => '退款成功，当前可用聊天时长',],
                        ['type' => 'text', 'red' => '101', 'text' => '0分钟',],
                        ['type' => 'text', 'red' => '102', 'text' => '畅聊已截止',],
                    ],
                ];
                $this->getMessageSendService()->sendSystemMessage($consulting, $consulting->memberId, ConsultingMessage::SYSTEM, $content);

                // 给律师消息
                if (is_id($consulting->lawyerId)) {
                    $content = [
                        'templates' => [
                            ['type' => 'text', 'red' => '102', 'text' => '用户退款成功,畅聊已截止',]
                        ],
                    ];
                    $this->getMessageSendService()->sendSystemMessage($consulting, $consulting->lawyerId, ConsultingMessage::SYSTEM, $content);
                }
            }


        } else {
            //次数

            $consulting->questionsNum -= $questionsNum;

            if ($consulting->questionsNum < 0) {
                $consulting->questionsNum = 0;
            }

            $consulting->questionsNumRemind = 0;
            $consulting->save();

            // 给用户消息
            $content = [
                'templates' => [
                    ['type' => 'text', 'red' => '102', 'text' => '退款成功，当前可用聊天次数为：',],
                    ['type' => 'text', 'red' => '101', 'text' => $consulting->questionsNum . '次',]
                ],
            ];
            $this->getMessageSendService()->sendSystemMessage($consulting, $consulting->memberId, ConsultingMessage::SYSTEM, $content);

            // 给律师消息
            if (is_id($consulting->lawyerId)) {
                $content = [
                    'templates' => [
                        ['type' => 'text', 'red' => '102', 'text' => '用户退款成功,剩余聊天次数为：' . $consulting->questionsNum . '次',]
                    ],
                ];
                $this->getMessageSendService()->sendSystemMessage($consulting, $consulting->lawyerId, ConsultingMessage::SYSTEM, $content);
            }
        }

    }

    private function getMessageSendService(): MessageSendService
    {
        return app(MessageSendService::class);
    }

}
